From: Taku Kudo Date: Sat, 20 Aug 2022 14:34:37 +0000 (+0900) Subject: added ShutdownLibrary function to uninitialize global variables X-Git-Tag: archive/raspbian/0.1.97-3+rpi1^2~5 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/%22/%22http:/www.example.com/cgi/%22?a=commitdiff_plain;h=768d48c996d3144513e9bcf100640a9ced58a6b6;p=sentencepiece.git added ShutdownLibrary function to uninitialize global variables Signed-off-by: Kentaro Hayashi Gbp-Pq: Name 0023-added-ShutdownLibrary-function-to-uninitialize-globa.patch --- diff --git a/src/compile_charsmap_main.cc b/src/compile_charsmap_main.cc index 13bf822..da15328 100644 --- a/src/compile_charsmap_main.cc +++ b/src/compile_charsmap_main.cc @@ -156,6 +156,7 @@ struct BinaryBlob { } // namespace sentencepiece int main(int argc, char **argv) { + sentencepiece::ScopedResourceDestructor cleaner; sentencepiece::ParseCommandLineFlags(argv[0], &argc, &argv, true); const std::vector #include "common.h" +#include "init.h" #include "sentencepiece_processor.h" #ifdef _USE_EXTERNAL_ABSL @@ -35,6 +36,7 @@ void Abort() { SetTestCounter(2); } else { std::cerr << "Program terminated with an unrecoverable error." << std::endl; + ShutdownLibrary(); exit(-1); } } @@ -43,6 +45,7 @@ void Exit(int code) { if (GetTestCounter() == 1) { SetTestCounter(2); } else { + ShutdownLibrary(); exit(code); } } diff --git a/src/init.h b/src/init.h index 090a2d9..7c75db2 100644 --- a/src/init.h +++ b/src/init.h @@ -18,6 +18,7 @@ #include "common.h" #include "third_party/absl/flags/flag.h" #include "third_party/absl/flags/parse.h" +#include "third_party/protobuf-lite/google/protobuf/message_lite.h" ABSL_DECLARE_FLAG(int32, minloglevel); @@ -35,6 +36,20 @@ inline void ParseCommandLineFlags(const char *usage, int *argc, char ***argv, logging::SetMinLogLevel(absl::GetFlag(FLAGS_minloglevel)); } + +inline void ShutdownLibrary() { + google::protobuf::ShutdownProtobufLibrary(); +#ifdef HAS_ABSL_CLEANUP_FLAGS + absl::CleanupFlags(); +#endif +} + +class ScopedResourceDestructor { + public: + ScopedResourceDestructor() {} + ~ScopedResourceDestructor() { ShutdownLibrary(); } +}; + } // namespace sentencepiece #endif // INIT_H_ diff --git a/src/spm_decode_main.cc b/src/spm_decode_main.cc index 3382ddc..bc49bd3 100644 --- a/src/spm_decode_main.cc +++ b/src/spm_decode_main.cc @@ -34,6 +34,7 @@ ABSL_FLAG(std::string, extra_options, "", "':' separated encoder extra options, e.g., \"reverse:bos:eos\""); int main(int argc, char *argv[]) { + sentencepiece::ScopedResourceDestructor cleaner; sentencepiece::ParseCommandLineFlags(argv[0], &argc, &argv, true); std::vector rest_args; diff --git a/src/spm_encode_main.cc b/src/spm_encode_main.cc index b0e508d..2fbb850 100644 --- a/src/spm_encode_main.cc +++ b/src/spm_encode_main.cc @@ -51,6 +51,7 @@ ABSL_FLAG(bool, generate_vocabulary, false, "Generates vocabulary file instead of segmentation"); int main(int argc, char *argv[]) { + sentencepiece::ScopedResourceDestructor cleaner; sentencepiece::ParseCommandLineFlags(argv[0], &argc, &argv, true); std::vector rest_args; diff --git a/src/spm_export_vocab_main.cc b/src/spm_export_vocab_main.cc index b5d93cb..e5b97df 100644 --- a/src/spm_export_vocab_main.cc +++ b/src/spm_export_vocab_main.cc @@ -1,11 +1,10 @@ - - // Copyright 2016 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// n// http://www.apache.org/licenses/LICENSE-2.0 +// +// http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, @@ -29,6 +28,7 @@ ABSL_FLAG(std::string, output_format, "vocab", "and scores, syms outputs pieces and indices."); int main(int argc, char *argv[]) { + sentencepiece::ScopedResourceDestructor cleaner; sentencepiece::ParseCommandLineFlags(argv[0], &argc, &argv, true); sentencepiece::SentencePieceProcessor sp; diff --git a/src/spm_normalize_main.cc b/src/spm_normalize_main.cc index 96da360..39f3ef9 100644 --- a/src/spm_normalize_main.cc +++ b/src/spm_normalize_main.cc @@ -46,6 +46,7 @@ using sentencepiece::normalizer::Builder; using sentencepiece::normalizer::Normalizer; int main(int argc, char *argv[]) { + sentencepiece::ScopedResourceDestructor cleaner; sentencepiece::ParseCommandLineFlags(argv[0], &argc, &argv, true); std::vector rest_args; diff --git a/src/spm_train_main.cc b/src/spm_train_main.cc index c34ee02..6ab634d 100644 --- a/src/spm_train_main.cc +++ b/src/spm_train_main.cc @@ -157,6 +157,7 @@ ABSL_FLAG(std::uint64_t, differential_privacy_clipping_threshold, 0, " clipping the counts for DP"); int main(int argc, char *argv[]) { + sentencepiece::ScopedResourceDestructor cleaner; sentencepiece::ParseCommandLineFlags(argv[0], &argc, &argv, true); sentencepiece::TrainerSpec trainer_spec; diff --git a/src/test_main.cc b/src/test_main.cc index b3170e2..38c978d 100644 --- a/src/test_main.cc +++ b/src/test_main.cc @@ -24,6 +24,7 @@ ABSL_FLAG(std::string, test_srcdir, "../data", "Data directory."); ABSL_FLAG(std::string, test_tmpdir, "test_tmp", "Temporary directory."); int main(int argc, char **argv) { + sentencepiece::ScopedResourceDestructor cleaner; sentencepiece::ParseCommandLineFlags(argv[0], &argc, &argv, true); sentencepiece::test::RunAllTests(); return 0;